import BaseConstants from '../constant/base_constants'
import ResultConstants from "../constant/result_constants"
import ObjectUtils from "../util/object_utils"

class ApiResult {
	constructor(code, message, state) {
		if (typeof code === typeof BaseConstants.EMPTY_OBJ && !ObjectUtils.isNUN(code)) {
			this.constructorJSON(code)
		} else {
			this.code = code
			this.message = message
			this.state = state
		}
	}

	constructorJSON(resp) {
		this.code = resp.code
		this.message = resp.message
		this.state = resp.state
	}

	isSuccess() {
		return this.code === ResultConstants.SUCCESS || this.code === ResultConstants.SUCCESS_SUBMIT
	}

	isFail() {
		return this.code === ResultConstants.COMMON_EX
	}

	isError() {
		return this.code === ResultConstants.COMMON_ERR
	}
}

export default ApiResult